package com.qzsoft.system.modules.sys.dict.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.qzsoft.jeemis.common.utils.Result;
import com.qzsoft.jeemis.repository.sys.entity.BasicCodeitemEntity;
import com.qzsoft.system.modules.sys.dict.service.SysDictService;
import com.qzsoft.zhjd.common.utils.ResultUtils;

/**
 * @ClassName SysDictController
 * @Description 字典表控制器
 * @author linhuawei
 * @since 2021-10-29
 */
@RestController
@RequestMapping("/api/sysDict")
public class SysDictController {

    @Autowired
    private SysDictService sysDictService;

    /**
     * @Title listByCodeid
     * @Description 根据字典类型主键获取字典列表
     * @param codeid 字典类型主键
     * @return 字典列表
     */
    @GetMapping("/listByCodeid")
    public Result<List<BasicCodeitemEntity>> listByCodeid(String codeid) {
        List<BasicCodeitemEntity> data = sysDictService.listByCodeid(codeid);
        return ResultUtils.ok(data);
    }

    /**
     * @Title list
     * @Description 根据字典类型主键获取字典列表
     * @param codeid 字典类型主键
     * @return 字典列表
     */
    @GetMapping("/list/{codeid}")
    public Result<List<BasicCodeitemEntity>> list(@PathVariable("codeid")String codeid) {
        List<BasicCodeitemEntity> data = sysDictService.listByCodeid(codeid);
        return ResultUtils.ok(data);
    }


    /**
     * @Title list
     * @Description 根据字典类型主键获取字典列表
     * @param codeid 字典类型主键
     * @return 字典列表
     */
    @GetMapping("/list")
    public Result<Map<String, List<BasicCodeitemEntity>>> listByCodeids(String codeids) {
        String[] strs = codeids.split(",");
        List<BasicCodeitemEntity> data;
        Map<String, List<BasicCodeitemEntity>> map = new HashMap<String, List<BasicCodeitemEntity>>();
        for(String str : strs) {
            if(ObjectUtils.isNotEmpty(str)) {
                data = sysDictService.listByCodeid(str);
                map.put(str, data);
            }
        }
        return ResultUtils.ok(map);
    }
}
